package cx3k.scheduler;

import java.util.ArrayList;
import java.util.Set;

//import cx3k.scheduler.objects.Course;
import cx3k.scheduler.objects.GroupedCourse;
import cx3k.scheduler.wrappers.GroupedCourseWrapper;
import cx3k.scheduler.objects.RequirementGrouping;

public class SchedulerCourse implements Comparable<SchedulerCourse>{	
	
	public int numOfClasses = 1;	

	private int leastCredit;
	private int creditsRequired;
	
	public boolean isGrouping;
	public int priority;
	public String degreeProgram;
	public String courseNum;
	public String[] courseNums;
	public String[] degreePrograms;
	public int numOfCoursesRequired = 1;	
	public int credit;
	public int[] credits;
	
	public ArrayList<SchedulerCourse> preReqs;
	
	
	public SchedulerCourse(RequirementGrouping myGrouping)
	{
		System.out.println("grouping = " + myGrouping.getRequirementGroupingId());
		
		preReqs = new ArrayList<SchedulerCourse>();
		this.creditsRequired = myGrouping.getCreditsRequired();
				
		Set<GroupedCourse> myGroup = myGrouping.getGroupedCourses();		
		
		if(myGroup.size() > 1)intializeMultipleClasses(myGroup);	
		else intializeOneClass(myGroup);
	}
	
	public SchedulerCourse(GroupedCourseWrapper myWrapper)
	{		
		preReqs = new ArrayList<SchedulerCourse>();
		this.credit = Integer.valueOf(myWrapper.getCourse().getCredits());
		this.degreeProgram = myWrapper.getCourse().getDegreeProgram();
		this.courseNum = myWrapper.getCourse().getCourseNum();
	
			
		/*
		for(GroupedCourse myCourse: myWrapper.getPreReqs())
		{			
			GroupedCourseWrapper newWrapper = new GroupedCourseWrapper(myCourse);	
			SchedulerCourse myPreReq = new SchedulerCourse(newWrapper);
			preReqs.add(myPreReq);
		}
		*/	
	}
	


	private void intializeOneClass(Set<GroupedCourse> mySet)
	{
		isGrouping = false;
		GroupedCourseWrapper newWrapper = null;
				
		for(GroupedCourse myCourse: mySet)
				newWrapper = new GroupedCourseWrapper(myCourse);
			
		oneCourseInfo(newWrapper);
	}	

	private void oneCourseInfo(GroupedCourseWrapper myWrapper)
	{	
		
		degreePrograms = new String[1];
		courseNums = new String[1];
		credits = new int[1];	
		
		this.credit = Integer.valueOf(myWrapper.getCourse().getCredits());
		this.degreeProgram = myWrapper.getCourse().getDegreeProgram();
		this.courseNum = myWrapper.getCourse().getCourseNum();
		
		credits[0] = credit;
		degreePrograms[0] = degreeProgram;
		courseNums[0] = courseNum;
		
		System.out.println(degreePrograms + " " + courseNums);
			
		
		for(GroupedCourse myCourse: myWrapper.getPreReqs())
		{
			GroupedCourseWrapper newWrapper = new GroupedCourseWrapper(myCourse);			
			SchedulerCourse myPreReq = new SchedulerCourse(newWrapper);
			preReqs.add(myPreReq);
		}	
	}
	
	private void intializeMultipleClasses(Set<GroupedCourse> mySet)
	{
		isGrouping = true;
		GroupedCourse myGCourse = null;
		int ctr = 0;
		
		
		
		degreePrograms = new String[mySet.size()];
		courseNums = new String[mySet.size()];
		credits = new int[mySet.size()];	
		
		System.out.println(degreePrograms + " " + courseNums);
		
		for(GroupedCourse myCourse: mySet)
		{
			GroupedCourseWrapper newWrapper = new GroupedCourseWrapper(myCourse);
			myGCourse = newWrapper.getCourse();
			addCourseInfo(myGCourse.getCredits(),myGCourse.getDegreeProgram(),myGCourse.getCourseNum(),ctr);
			
			//get prerequisites from first course in the set
			if(ctr == 0)
			{
				for(GroupedCourse myCourse1: newWrapper.getPreReqs())
				{
					GroupedCourseWrapper myWrapper = new GroupedCourseWrapper(myCourse1);			
					SchedulerCourse myPreReq = new SchedulerCourse(myWrapper);
					preReqs.add(myPreReq);
				}	
			}			
			ctr++;			
		}
		numOfClasses = ctr;	
		credit = leastCredit;
		numOfCoursesRequired = creditsRequired / leastCredit;
	}	
		
	private void addCourseInfo(String credit, String degreeProgram, String courseNum, int ctr)
	{
		
		if(ctr == 0){
			this.degreeProgram = degreeProgram;
			this.courseNum = courseNum;
			this.credit = Integer.valueOf(credit);
		}
		
		this.credits[ctr] = Integer.valueOf(credit);
		
		for(int x = 0; x < ctr; x++)
			if(credits[ctr] <= credits[x])
				leastCredit = credits[ctr];
		
		this.degreePrograms[ctr] = degreeProgram;
		this.courseNums[ctr] = courseNum;
	}
	
	@Override
	public boolean equals(Object myCourse)
	{		
		
		SchedulerCourse myC = (SchedulerCourse) myCourse;
		if(!isGrouping)
		{
			if(myC.degreeProgram.equals(degreeProgram) && myC.courseNum.equals(courseNum))return true;
			else return false;
		}
		else{
			return false;
		}
		//return true;
		

	}
	
	public int compareTo(SchedulerCourse arg0) {
		
		if(arg0.priority < this.priority)
			return -1;
		else if(arg0.priority == this.priority)
			return 0;
		else
			return 1;
	}

	
	public String toString()
	{
		String myPreReqs = "";
		
		for(int x = 0; x < preReqs.size(); x++)
			myPreReqs = myPreReqs + preReqs.get(x).degreeProgram + " " + preReqs.get(x).courseNum + ", ";
		
		if(!isGrouping)	return degreeProgram + " " + courseNum + ": " + credit + " Priority: " + priority + " Prereqs: " + myPreReqs;
		else {			
			String myString = "";
			for(int x = 0; x < numOfClasses; x++)
				myString = myString + degreePrograms[x] + " " + courseNums[x] + ", ";			
			
			return myString + " Priority: " + priority +" Prereeqs: " + myPreReqs + " Take:" + numOfCoursesRequired;
		}
	}
}
